# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#

# 复核时，需要冻结所有的界面元素，不允许用户编辑
checking_freeze = (form) ->
    in_checking = form.form 'get value', 'in_checking'
    return if in_checking isnt 'true'

    # 修改背景色，以区分复核及录入
    $('body').css('background-color', 'black')
    freeze_form form


# 根据用户当前的选择，实时改变界面的状态(隐藏、显示、禁用...)
update_status = (form, record) ->
    inside = form.find('.dropdown.inside').dropdown 'get value'
    chnl   = form.find('.dropdown.chnl').dropdown 'get value'
    return if not inside or not chnl

    $settl_merid = form.find('.dropdown.settl_merid')
        .addClass('disabled').dropdown 'clear'
    $mapto_merid = form.find('.dropdown.mapto_merid')
        .addClass('disabled').dropdown 'clear'
    $settl_merid = form.find('.dropdown.settl_merid')
        .addClass('disabled').dropdown 'clear'

    $unit = form.find('.dividing.unit, .fields.unit').hide()
    $trctl = form.find('.dividing.trctl, .fields.trctl').hide()

    # 清算商户只需要录入最基本的信息
    if parseInt(chnl) == 0
        # 清除所有控件的值
        for clear in [ $unit, $trctl ]
            clear.find('.dropdown').dropdown 'clear'
            clear.find('.checkbox').checkbox 'uncheck'
            clear.find('input').val ''
        return

    # 非清算可以指定清算商户
    $settl_merid.removeClass('disabled')

    # 非清算平台商户可以指定外联商户
    if inside is 'true'
        $mapto_merid.addClass 'loading'
        $.post '/merch/ajax/mapto_merids', { chnl: chnl }, (resp) ->
            $mapto_merid.removeClass 'loading'
            return if not resp.succ
            items = '<div class="item" data-value=" ">无</div>'
            for item in resp.records
                color = if item.shaded then 'orange' else '#888'
                items += "<div class='item' data-value='#{item.uuid}'>"
                items += "<div class='right floated'
                    style='font-size:small; color:#{color};'>#{item.merno}</div>"
                items += "#{item.name.substring(0, 25)}"
                items += "</div>"
            $mapto_merid.find('.menu').html items
            $mapto_merid.removeClass('disabled').dropdown 'refresh'

            # 在 Ajax 请求后恢复原来的值(编辑时)
            if record and record.mapto_merid
                $mapto_merid.dropdown 'set selected', record.mapto_merid

            checking_freeze form    # 如果是复核，冻结界面元素

    # 非清算商户可以提供公司信息和开户帐户信息
    $unit.show()
    $trctl.show()


# 设置交易权限值
update_trctl_values = (form, trctl) ->
    # 交易权限
    form.find('.checkbox.trcode').checkbox 'uncheck'
    $.each trctl.trcodes, (k, v) ->
        form.find(".checkbox.trcode.#{k}").checkbox 'check'

    # 交易限制
    for trcode in [ 't20', 't60', 'tz0' ]
        continue if not trctl[trcode]
        for field in window.trctl_fields
            form.find(".fields.trctl.#{trcode} input[name=#{field}]")
                .val(trctl[trcode][field] or '')

$ ->
    form = $('form.main')
    form.form
        inline: true,
        fields:
            inside:         optional: false, rules: [ type: 'empty' ]
            chnl:           optional: false, rules: [ type: 'empty' ]
            merno:          optional: false, rules: [
                { type: 'empty' }, { type: 'doesntContain[ ]' }
            ]
            t20_fee:        optional: false, rules: [ type: 'empty' ]
            name:           optional: false, rules: [ type: 'empty' ]
            contact:        optional: false, rules: [ type: 'empty' ]
            address:        optional: false, rules: [ type: 'empty' ]
            email:          optional: true, rules: [ type: 'email' ]
            telephone:      optional: true, rules: [ type: 'number' ]
            mobile:         optional: true, rules: [ type: 'number' ]
            fax:            optional: true, rules: [ type: 'number' ]
            unit_assets:    optional: true, rules: [ type: 'number' ]

    checking_freeze form    # 如果是复核，冻结界面元素

    # 页面数据集
    record = form.form 'get value', 'record'
    if record and record.trim().length > 0
        form.form 'set value', 'record', ''
        record = JSON.parse record
        record.chnl = '' + record.chnl
    else
        record = null

    # 平台/外联商户选择改变
    form.find('.dropdown.inside').dropdown
        onChange: (value, text, $choice) ->
            update_status form, record

    # 交易渠道选择改变
    form.find('.dropdown.chnl').dropdown
        onChange: (value, text, $choice) ->
            # 清算商户不分平台/外联
            if parseInt(value) == 0
                form.find('.dropdown.inside')
                    .dropdown('set selected', 'true').addClass('disabled')
            else
                form.find('.dropdown.inside').removeClass 'disabled'
            update_status form, record

    # 交易权限全选按钮事件
    form.find('.ui.button#trcode_all').click ->
        form.find('.checkbox.trcode').each ->
            $(this).checkbox 'check'

    # 交易权限全不选按钮事件
    form.find('.ui.button#trcode_none').click ->
        form.find('.checkbox.trcode').each ->
            $(this).checkbox 'uncheck'

    # 交易控制模板改变
    form.find('.dropdown.trctl_t').dropdown
        onChange: (value, text, $choice) ->
            $this = $(this).addClass 'loading'
            $.post '/merch/ajax/trctl_info', {
                trctl_uuid: value
            }, (resp) ->
                $this.removeClass 'loading'
                return if not resp.succ
                if resp.record.trctl
                    update_trctl_values form, resp.record.trctl

    # 录入删除按钮事件
    form.find('button#input_delete').click (evt) ->
        if $(this).hasClass('has_clicked')
            $(this).removeClass('has_clicked').text('删 除')
            $(this).addClass 'loading'
            $.post '/merch/input/delete', {
                serial: form.form('get value', 'serial'),
                uuid: form.form('get value', 'uuid')
            }, (resp) ->
                form.find('button#input_delete').removeClass 'loading'
        else
            $(this).addClass('has_clicked').text('确认删除')

    # 复核通过按钮事件
    form.find('button#pass').click ->
        if $(this).hasClass('has_clicked')
            effect_time = form.form 'get value', 'effect_time'
            if not effect_time or effect_time.trim().length == 0
                alert '请选择信息生效的时间'
                return
            $(this).removeClass('has_clicked').text('通 过')
            form.attr 'action', '/merch/check/pass'
            form.submit()
        else
            form.find('button#reject').text('拒 绝').removeClass('has_clicked')
            form.find('.fields#reject_reason').hide()
            form.find('.fields#effect_time').show()
            $(this).addClass('has_clicked').text('确认通过')

    # 复核拒绝按钮事件
    form.find('button#reject').click ->
        if $(this).hasClass('has_clicked')
            reason = form.form 'get value', 'reject_reason'
            if not reason or reason.trim().length == 0
                alert '请输入拒绝的原因，以便录入人员可以有针对性的进行修改'
                return
            $(this).removeClass('has_clicked').text('拒 绝')
            form.attr 'action', '/merch/check/reject'
            form.submit()
        else
            form.find('button#pass').text('通 过').removeClass('has_clicked')
            form.find('.fields#effect_time').hide()
            form.find('.fields#reject_reason').show()
            $(this).addClass('has_clicked').text('确认拒绝')

    # 恢复界面的值(编辑时)
    if record
        form.form 'set values', record
        update_trctl_values form, record.trctl if record.trctl

    # 提交前处理
    form.submit (evt) ->
        return evt.preventDefault() if not $(this).form('is valid')

        # 组织交易控制数据
        trctl = {}

        # 交易权限
        trctl.trcodes = {}
        form.find('.checkbox.trcode').each ->
            if $(this).checkbox 'is checked'
                trctl.trcodes[$(this).find('input').attr('name')] = true

        # 交易限制
        for trcode in [ 't20', 't60', 'tz0' ]
            trctl[trcode] = {}
            for field in window.trctl_fields
                val = form.find(".fields.trctl.#{trcode} input[name=#{field}]").val()
                trctl[trcode][field] = val if val and val.trim().length > 0

        form.form 'set value', 'trctl', JSON.stringify trctl
